/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.cin.stadium.application.activities;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import br.cin.stadium.application.R;
import br.cin.stadium.middleware.StadiumException;
import br.cin.stadium.middleware.StadiumTS;
import br.cin.stadium.middleware.media.Media;

/**
 * A grid that displays a set of framed photos.
 * 
 */
public class VideoGridList extends Activity
{	
	private static final int DIALOG_ADD_SECTOR = 1;
	AlertDialog alertDialogSector = null;

	private String mediaName = "";	

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.video_grid_list);
		setTitle(R.string.video_grid_list_title);

		GridView g = (GridView) findViewById(R.id.grid_video_list);
		g.setAdapter(new ImageAdapter(this));
	}

	public class ImageAdapter extends BaseAdapter
	{
		
		private Context mContext;		
		private String[] texts = {getResources().getString(R.string.airbag), getResources().getString(R.string.carcrash)};
		
		public ImageAdapter(Context c)
		{
			mContext = c;
		}

		public int getCount()
		{
			return texts.length;
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			TextView textView;
			if (convertView == null)
			{
				textView = new TextView(mContext);
				textView.setLayoutParams(new GridView.LayoutParams(85, 85));
//				textView.setAdjustViewBounds(false);
//				textView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				textView.setPadding(8, 8, 8, 8);
			} else
			{
				textView = (TextView) convertView;
			}

			textView.setText(texts[position]);
			
			textView.setOnClickListener(new OnClickListener() 
			{
				public void onClick(View v) 
				{
					TextView tv = (TextView) v;
					mediaName = tv.getText().toString();
					showDialog(DIALOG_ADD_SECTOR);
				}
			});

			return textView;
		}
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        	case DIALOG_ADD_SECTOR:
                // This example shows how to add a custom layout to an AlertDialog
                LayoutInflater factory = LayoutInflater.from(this);
                final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
                alertDialogSector = new AlertDialog.Builder(VideoGridList.this)
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(R.string.alert_dialog_add_title)
                    .setView(textEntryView)
                    .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            write(mediaName, ((EditText) alertDialogSector.findViewById(R.id.sector_edit)).getText().toString());
                        }
                    })
                    .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            //empty
                        }
                    })
                    .create();
                return alertDialogSector;
        }
        return null;
	}
	
	private void write(String mediaName, String sector)
	{
		Media media = new Media();
		media.setName(mediaName);
		media.setStadiumSector(Integer.parseInt(sector));
		media.setType("3gp");
		TextView msgTextView = null;
		AssetManager am = getAssets();
			//this.getResources().openRawResource(Integer.parseInt(mediaName));
        byte[] buffer = null;
        try
        {
        	InputStream is = am.open(mediaName + ".3gp");
            buffer = new byte[is.available()];
            is.read(buffer);
        } catch (IOException e)
        { 
            int i = 0;
        }
		media.setContent(buffer);
		try {
			StadiumTS.getInstance().write(media);
		} catch (StadiumException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}